package com.example.bbsbe.Repository;

import com.example.bbsbe.entity.profile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ProfileRepository extends JpaRepository<profile, Long> {
    //long countByOwnerIdAndCondition(String owner, String number);

   // Integer sumViewsByOwnerIdAndCondition(String owner, String number);
   Optional<profile> findById(Long Id);
    @Query("SELECT SUM(p.views) FROM profile p WHERE p.owner = :ownerId AND p.condition = :condition")
    Integer calculateTotalViews(
            @Param("ownerId") String ownerId,
            @Param("condition") String condition
    );
    @Query("SELECT count(p.id) FROM profile p WHERE p.owner = :ownerId AND p.condition = :condition")
    Integer calculateTotalProfiles(
            @Param("ownerId") String ownerId,
            @Param("condition") String condition
    );
    @Query("SELECT SUM(p.discuss) FROM profile p WHERE p.owner = :ownerId AND p.condition = :condition")
    Integer calculateTotalDiscuss(
            @Param("ownerId") String ownerId,
            @Param("condition") String condition
    );
    @Modifying
    @Query("UPDATE profile p SET p.views = p.views + 1 WHERE p.id = :profileId")
     void incrementViews(@Param("profileId") Long profileId);

 List<profile> findByCondition(String number);

 long countByCondition(String number);

 long countByTypeAndCondition(String proType, String number);

 List<profile> findByTypeAndCondition(String proType, String number);

 @Modifying
 @Query("UPDATE profile p SET p.discuss = p.discuss + :increment WHERE p.id = :profileId")
 void incrementDiscuss(@Param("profileId") Long profileId, @Param("increment") int increment);

 List<profile> findByOwner(String owner);
}

